package com.puppy.uhfreader;

import UHF.Reader18;
import UHF18.UHFReader;
//import com.puppy.hik.TwoCamerasWindow;
import com.puppy.util.PingUtil;

import java.awt.EventQueue;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/*
 * @Author: puppy
 * @CreateDate: 2023-06-17 17:09:00
 * @FullNameClass: $path$
 * @Description:
 * @path com.puppy.uhfreader.mainNoFrame
 */
public class MainNoFrame2 {
//    private String[] bauds = {"9600bps","19200bps","38400bps","57600bps","115200bps"};
//    private String[] powers={"0 dBm","1 dBm","2 dBm","3 dBm","4 dBm","5 dBm","6 dBm","7 dBm","8 dBm","9 dBm",
//            "10 dBm","11 dBm","12 dBm","13 dBm","14 dBm","15 dBm","16 dBm","17 dBm","18 dBm","19 dBm",
//            "20 dBm","21 dBm","22 dBm","23 dBm","24 dBm","25 dBm","26 dBm","27 dBm","28 dBm","29 dBm","30 dBm"};

//    private final static String UHFAddr = "192.168.203.35";
//    private final static String UHFPort = "22347";
    private static String ipaddr="192.168.203.35";
    private static int Port = 22347;
    private static int delay = 5;
    private static UHFReader uhf = new UHFReader();
    private static boolean flag = false;
    private static String[] EPC;
    private static String sepc;
    private static Properties properties = new Properties();
    static {
        try {
            properties.load(new FileInputStream("src/main/resources/config.properties"));
            ipaddr = properties.getProperty("ipaddr").toString();
            Port = Integer.parseInt(properties.getProperty("port"));
            delay = Integer.parseInt(properties.getProperty("delay"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static int i = uhf.OpenByTcp(ipaddr, Port);
    private static ArrayList<Account> data = new ArrayList<>();
    private static String temp[][] = {{"1","1","1"},{"1","1","1"}};

    public static void main(String[] args) throws InterruptedException,IOException {



/*        try {
            properties.load(new FileInputStream("src/main/resources/config.properties"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }*/
        Runnable runnable = new Runnable() {
            @Override
            public void run() {


                try {
                    if(PingUtil.isReachAble(ipaddr)){
                        if(flag){
                            flag = false;
//                            UHFReader temp = new UHFReader();
                            System.out.println("网通了！！net can connect RFID!!!");
                            /**
                             * 网络连接，断网后须关掉端口再打开端口
                             */
                            Reader18 reader18 = uhf.getReader18();
                            reader18.CloseNetPort(Port);

                            i = uhf.OpenByTcp(ipaddr, Port);
//                            System.loadLibrary("UHF_Reader18");
//                            reader18 = new Reader18();
//                            UHFReader uhfReader = new UHFReader();
//                            uhfReader.toTnt(reader18);
                        }
                    }else {
                        System.out.println("网断了！！！！net cant connect RFID!!!!");
                        flag = true;
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

                /*try {
                    if(!PingUtil.isReachAble(ipaddr)){
                        Runtime.getRuntime().addShutdownHook(new Thread() {
                            public void run() {

                                try {
                                    Runtime.getRuntime().exec("java -jar UHFReader.jar");
                                    Thread.sleep(10000);
                                } catch (IOException | InterruptedException e) {
                                    throw new RuntimeException(e);
                                }

                            }

                        });
//                        System.exit(0);
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }*/
                EPC=uhf.Inventory();
                if(EPC != null ){
                    for(int m=0;m<EPC.length;m++  ) {
                        /**
                         * sepc:判断为空or（不为空并且与RFID接收数据不一致）
                         * 为空：代表第一次
                         * 不为空并且与RFID接收数据不一致：则需要重新加载摄像，如果一致则不切换摄像头。
                         */
                        if (sepc == null || (sepc != null && (!sepc.equals(EPC[m])))){
                            sepc = EPC[m];
                            System.out.println("卡号:" + sepc);
                            data.clear();
                            try{
                                String accouts = properties.get(sepc).toString();
                                String[] element = accouts.split("&");
                                temp[0][0] = element[0].toString();
                                temp[0][1] = element[1].toString();
                                temp[0][2] = element[2].toString();
                                temp[1][0] = element[3].toString();
                                temp[1][1] = element[4].toString();
                                temp[1][2] = element[5].toString();
//                                TwoCamerasWindow.run(temp);
//                                        args= null;
                            }catch (Exception ex){
//                                ex.printStackTrace();
                                System.out.println("卡号不对！！！！！！！！！\n卡号不对！！！！！！！！！\n卡号不对！！！！！！！！！\n卡号不对！！！！！！！！！\n卡号不对！！！！！！！！！\n卡号不对！！！！！！！！！\n卡号不对！！！！！！！！！\n");
                            }
                        }
                    }
                }
//                uhf.CloseByTcp(i);
            }
        };
        EventQueue.invokeLater(runnable);
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();

        service.scheduleAtFixedRate(runnable,3000,delay, TimeUnit.MILLISECONDS);
    }
    private static void Inventory()
    {
        EPC=uhf.Inventory();
        if(EPC != null )
        {
            for(int m=0;m<EPC.length;m++  )
            {
                String sepc=EPC[m];
                boolean inlist=false;
            }
        }

    }
}

